using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Biz;
using EntLibEC.Biz.Online;

namespace EntLibEC.View.Online
{
	/// <summary>
	/// Summary description for ProductQuestionList.
	/// </summary>
	public partial class ProductQuestionList : PageBase
	{
		protected EntLibEC.View.Cmn.Category ucCategory;
		protected System.Web.UI.WebControls.DataGrid dgProductRemark;
		protected EntLibEC.View.Cmn.StockProduct ucProduct;
		protected EntLibEC.View.Cmn.PM ucPM;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			dgProductQuestion.PageSize = AppConst.PageSize;
			dgProductQuestion.PagerStyle.PageButtonCount = AppConst.PageButtonCount;

			txtDateFrom.Attributes["onclick"]= "javascript:calendar()";
			txtDateTo.Attributes["onclick"]= "javascript:calendar()";			

			if ( !Page.IsPostBack )
			{
				ucCategory.BindCategory();	
				ucPM.BindPM(true);
				bindGrid();	

			}
		}

		private bool checkForm()
		{
			if ( txtDateFrom.Text.Trim() != "" && !Util.IsDate(txtDateFrom.Text.Trim()))
			{
				Assert(lblMessage, "DateFrom  format error", -1);
				return false;
			}
			if ( txtDateTo.Text.Trim() != "" && !Util.IsDate(txtDateTo.Text.Trim()))
			{
				Assert(lblMessage, "DateTo  format error", -1);
				return false;
			}
			return true;
		}

		private void bindGrid()
		{
			if (!checkForm())
              return;
			Hashtable ht = new Hashtable(5);
			if (txtDateFrom.Text.Trim() != "")
				ht.Add("DateFrom",txtDateFrom.Text.Trim());
			if (txtDateTo.Text.Trim() != "")
				ht.Add("DateTo",txtDateTo.Text.Trim());

			if ( ucCategory.C3SysNo != AppConst.IntNull )
				ht.Add("Category", " c3sysno= " + ucCategory.C3SysNo);
			else if ( ucCategory.C2SysNo != AppConst.IntNull )
				ht.Add("Category", " c2sysno= " + ucCategory.C2SysNo);
			else if ( ucCategory.C1SysNo != AppConst.IntNull )
				ht.Add("Category", " c1sysno= " + ucCategory.C1SysNo);

			if (ucProduct.ProductSysNo !=AppConst.IntNull)
				ht.Add("ProductSysNo", ucProduct.ProductSysNo);
			if (ddlStatus.SelectedIndex > 0)
				ht.Add("Status",ddlStatus.SelectedValue);
			if (ucPM.PMSysNo != AppConst.IntNull)
				ht.Add("PMSysNo",ucPM.PMSysNo);

			DataSet ds = ProductQuestionManager.GetInstance().GetProductQuestionList(ht);
			ds =ConvertDs(ds);
            base.SetDataGridPage(ds, dgProductQuestion);

			dgProductQuestion.DataSource =ds;			
			dgProductQuestion.DataBind();

			int rows = 0;
			if ( Util.HasMoreRow(ds) )
				rows = ds.Tables[0].Rows.Count;
			Assert(lblMessage, rows.ToString() + " record(s)", 1);

		}

		private DataSet ConvertDs(DataSet ds)
		{
			if (!Util.HasMoreRow(ds))
		       return ds;
			
			ds.Tables[0].Columns.Add("StatusName");
			ds.Tables[0].Columns.Add("ProductIDShow");
			ds.Tables[0].Columns.Add("Reply");
			ds.Tables[0].Columns.Add("CustomerIDShow");
			ds.Tables[0].Columns.Add("CreateTimeShow");

			foreach( DataRow dr in ds.Tables[0].Rows )
			{
				dr["CreateTimeShow"] = Util.TrimDateNull(dr["CreateTime"]).ToString(AppConst.DateFormatLong);
				dr["StatusName"] = AppEnum.GetProductQuestionStatus(Util.TrimIntNull(dr["Status"]));
				dr["ProductIDShow"] =  dr["ProductID"].ToString() ;	
				if (dr["CustomerSysNo"].ToString().Trim()!="") 
				    dr["CustomerIDShow"] = "<a href=\"javascript:openWindowS2('../Basic/CustomerOpt.aspx?sysno=" + dr["CustomerSysNo"].ToString() + "&Opt=update')\">" + dr["CustomerID"].ToString() +  "</a>";			    
				string Question = Util.TrimNull(dr["Question"]);
				if ( Question.Length>50)
					dr["Question"] = "<a href=\"javascript:openWindowS2('ProductQuestionOpt.aspx?sysno=" + dr["SysNo"].ToString() + "')\">" + "<div title='" + Question + "'>" + Question.Substring(0,50)+"...</div>" +  "</a>";
				else
					dr["Question"] = "<a href=\"javascript:openWindowS2('ProductQuestionOpt.aspx?sysno=" + dr["SysNo"].ToString() + "')\">" + "<div title='" + Question + "'>" + Question + "</div>"+  "</a>";

				if (Util.TrimNull(dr["ReplyTime"])!=AppConst.StringNull)
				{
				   dr["Reply"] = Util.TrimNull(dr["ReplyUserName"]) + "[" + Util.TrimDateNull(dr["ReplyTime"]).ToString(AppConst.DateFormatLong) + "]";
				}
				
			}
			return ds;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.dgProductQuestion.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgProductQuestion_PageIndexChanged);

		}
		#endregion

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			bindGrid();
		}

		private void dgProductQuestion_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			dgProductQuestion.CurrentPageIndex = e.NewPageIndex;
			bindGrid();
		}



	}
}
